package sg.nus.iss.vmcs.gui.elements;

import java.awt.Button;
import java.awt.Component;
import java.awt.Label;
import java.awt.event.ActionListener;

/**
 * Button Item used in the CoinDisplay Dialog<br><br>
 * Modifications:
 * <li>30/12/2010 - Philip - Introduced the Item Display Strategy to standardize item display</li>
 * <li>1/1/2011 - Philip - Updated to become StoreViewerItem</li>
 * <br><br>
 * @author Pang Pingli
 * @author Philip Chew
 */
public class ButtonItem extends StoreViewerItem {

	/** Generated Serial ID */
	private static final long serialVersionUID = 4553476010974422136L;

	/** Button that activates the display */
	private Button	btn;
	
	/** Label that displays the value */
	private Label	value;
	
	/**
	 * Creates a new instance of the button item
	 * @param bn 	The name of the button
	 * @param ids	Display format of the UI elements
	 */
	public ButtonItem(String bn, ItemDisplayStrategy ids) {
		super(ids);
		
		//Setting up UI
		btn 	= new Button();
		value	= new Label();
		
		setName(bn);
		setValue("");
		ids.setupPanelLayout(this, btn, value);
	}

	/* 
	 * Returns Label to be set
	 */
	@Override
	public Component getDisplayComponent() {
		return value;
	}

	/* 
	 * Executes Variable Display State settings
	 */
	@Override
	public void setDisplay() {
		if(vds != null) vds.setDisplayComponent(this);		
	}

	@Override
	public void setName(String name) {
		btn.setLabel(name);
	}

	@Override
	public void setQuantity(int qty) {
		setValue(qty);				
	}

	@Override
	public void setValue(String value) {
		if(! value.equals("")) {
			this.value.setText(value);
		}
	}
	
	/**
	 * Clears the background color
	 */
	public void clear() {
		setDisplay();
	}

	@Override
	public void addListener(ActionListener listener) {
		btn.addActionListener(listener);
	}

	@Override
	public void setActionCommand(String command) {
		btn.setActionCommand(command);
	}


	@Override
	public void setActive(boolean active) {
		btn.setEnabled(active);
		value.setEnabled(active);
	}

	/* (non-Javadoc)
	 * @see sg.nus.iss.vmcs.models.StoreItemObserver#notifyChange(java.lang.String, int, int)
	 */
	@Override
	public void notifyChange(String name, int qty, int value) {
		this.setName(name);
		this.setValue(qty);
	}
}